package moe.service;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import moe.config.Configuration;
import moe.config.Constants;
import moe.config.PropertiesUtil;
import moe.util.JSONBuilder;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

public class CommonHandler extends AbstractHandler {
	
	private static final String[] acceptedConfig =
			new String[] {"port", "open_browser", "collect_pattern", "page_size", "tips_number", "preview_height", "zoom_rate", "session_expires", "server_cache", "default_filter"};
	
	public void config(HttpContext context, Map<String, String[]> query) {
		HttpResponse response = (HttpResponse) context.getAttribute(ExecutionContext.HTTP_RESPONSE);
		JSONBuilder builder = JSONBuilder.build().callback(getCallback(query));
		
		for (String field : PropertiesUtil.getPropertyKeys()) {
			if (Configuration.isPrivacy(field)) {
				continue;
			}
			String value = PropertiesUtil.getProperty(field);
			if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
				builder.add(field, Boolean.parseBoolean(value));
			} else if (value.matches("\\d+")) {
				try {
					builder.add(field, Long.valueOf(value));
				} catch (NumberFormatException e) {
					builder.add(field, value);
				}
			} else {
				builder.add(field, value);
			}
		}
		if (PropertiesUtil.hasProperty(Constants.COLLECTION_LIBRARY)) {
			builder.add("hasCollection", true);
		}
		
		sendNocacheJsonResponse(response, builder.buildString());
	}
	
	public void setConfig(HttpContext context, Map<String, String[]> query) {
		HttpResponse response = (HttpResponse) context.getAttribute(ExecutionContext.HTTP_RESPONSE);
		JSONBuilder builder = JSONBuilder.build().callback(getCallback(query));
		
		for (String key : acceptedConfig) {
			if (query.get(key) != null) {
				PropertiesUtil.setProperty(key, query.get(key)[0]);
			}
		}
		
		builder.add("done", true);
		sendNocacheJsonResponse(response, builder.buildString());
	}
	
	public void saveConfig(HttpContext context, Map<String, String[]> query) {
		HttpResponse response = (HttpResponse) context.getAttribute(ExecutionContext.HTTP_RESPONSE);
		JSONBuilder builder = JSONBuilder.build().callback(getCallback(query));
		
		PropertiesUtil.save();
		
		builder.add("done", true);
		sendNocacheJsonResponse(response, builder.buildString());
	}
	
	public void reloadConfig(HttpContext context, Map<String, String[]> query) {
		HttpResponse response = (HttpResponse) context.getAttribute(ExecutionContext.HTTP_RESPONSE);
		JSONBuilder builder = JSONBuilder.build().callback(getCallback(query));
		
		PropertiesUtil.loadInitConf();
		
		builder.add("done", true);
		sendNocacheJsonResponse(response, builder.buildString());
	}
	
	protected void sendNocacheJsonResponse(HttpResponse response, String json) {
		StringEntity entity = null;
		try {
			entity = new StringEntity(json, "application/json", "UTF-8");
		} catch (UnsupportedEncodingException e) {
			response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			return;
		}
		response.setStatusCode(HttpStatus.SC_OK);
		response.addHeader(HttpHeaders.CACHE_CONTROL, "no-cache");
		response.setEntity(entity);
	}
}
